<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>
</html><extend name="Public/base"/>

<block name="style">
    <link rel="stylesheet" href="__STATIC__/styles/vendors/bootstrap-table.css">
    <link rel="stylesheet" href="__STATIC__/styles/data_manage.css">
    <link rel="stylesheet" href="__STATIC__/styles/vendors/bootstrap-select.min.css">
    <link rel="stylesheet" href="__STATIC__/plugins/dropper/jquery.fs.dropper.min.css">
    <style>
        table {table-layout:fixed;}
    </style>
</block>

<block name="content">

    <div id="headbar">

        <!--<button id="group-import" class="btn btn-primary btn-flat" style="margin: 5px">-->
            <!--导入资料-->
        <!--</button>-->
    </div>

    <div id="treeContrainer">
        <div id="tree-container" class="pre-scrollable" style="max-height: none" >
            <div id="tree"  ></div>

        </div>
        <div id="table-container">
            <div id="placeHolder">
                点击左侧分类进行查询
            </div>
            <table id="table" style="display: none">
            </table>
        </div>
    </div>




    <div class="modal fade" id="complex_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="container-fluid">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                         <h4 class="modal-title" id="title_name" style="text-align:center;">title</h4>
                    </div>
                    <div class="modal-body">
                        <table id="datum_table" class="table table-condensed table-bordered">

                        </table>
                        <div class="control-group row enclosureDiv">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 left">
                                <label class="control-label" >附件:</label>
                            </div>
                        </div>
                        <div id="enclosureDiv" class="control-group row enclosureDiv">
                            <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9 right">
                                <div class="controls">
                                    <ul id="enclosures">

                                    </ul>
                                </div>
                            </div>
                        </div>

                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default btn-flat" data-dismiss="modal">关闭</button>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="import_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="container-fluid">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                        <h4 class="modal-title">资料导入</h4>
                    </div>
                    <form id="importForm" enctype="multipart/form-data" action="{:U('import')}" method="post">
                        <div class="modal-body">

                            <div class="control-group row">
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 left">
                                    <label class="control-label">类型:</label>
                                </div>
                                <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9 right">
                                    <div class="controls">
                                        <select name="class_id" id="class_id" class="form-control">
                                            <volist name="dicClasses" id = "dicClass" offset="0" length="7">
                                                <option value="{$dicClass.class_id}">{$dicClass.type_name}</option>
                                            </volist>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="control-group row">
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 left">
                                    <label class="control-label" >文件:</label>
                                </div>
                                <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9 right">
                                    <div class="controls">
                                        <input type="file" id="file_import" class="form-control" name="file_import">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="submit" id="importButton" class="btn btn-danger">导入</button>
                            <button type="button" class="btn btn-default btn-flat" data-dismiss="modal">关闭</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="confirm_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">确认</h4>
                </div>
                <div class="modal-body">
                    确定要删除吗?
                </div>
                <div class="modal-footer">
                    <button type="button" id="commonDel" class="btn btn-danger">删除</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                </div>
            </div>
        </div>
    </div>
</block>

<block name="script">

    <script src="__STATIC__/scripts/vendors/bootstrap-select.min.js"></script>
    <script src="__STATIC__/scripts/vendors/bootstrap-table.js"></script>
    <script src="__STATIC__/plugins/dropper/jquery.fs.dropper.min.js"></script>
    <script src="__STATIC__/scripts/vendors/bootstrap-treeview.min.js"></script>
    <script src="__STATIC__/plugins/dropper/jquery.fs.dropper.min.js"></script>
    <script src="__STATIC__/scripts/vendors/fileinput.min.js"></script>
    <script src="__STATIC__/scripts/vendors/jquery.form.min.js"></script>

    <script>
        var $table = $('#table');
        var $tree = $('#tree');
        var $datumTable = $('#datum_table');
        var datumDicClass;
        var datumDicType;
        var datumDicData;
        var oTreeHelper, tableHelper;
        var $complexModal = $('#complex_modal');
        var $importModal = $('#import_modal');
//        var selectClassId = false;



        window.operateEvents = {
            'click .name_click':function(e, value, row, index){
                $complexModal.modal('show');
                initComplexModal(row);
            }
        };


        function initComplexModal(data){
            $datumTable.empty();
            if (data.name != null) {
                $('#title_name').text(data.name);
            }
            for(var p in tableHelper.currentFields){
                var field = tableHelper.currentFields[p].field;
                var title = tableHelper.currentFields[p].title;
                if(data[field]!=undefined){
                    var val = data[field];
                    switch (field){
                        case 'chech_status':
                            if(val == -1){
                                data[field] = '审核未通过';
                            }else if(val == 0){
                                data[field] = '未审核';
                            }else if(val == 1){
                                data[field] = '审核通过';
                            }
                            break;
                        case 'version_flag':
                            if(val == 0){
                                data[field] = '发布';
                            }else if(val == 1){
                                data[field] = '修订';
                            }
                            break;
                        case 'operation_flag':
                            if(val == 0){
                                data[field] = '没有';
                            }else if(val == 1){
                                data[field] = '有';
                            }
                            break;
                    }
                    var html = '<tr><td>'+ title+  '</td><td>'+ data[field]+ '</td></tr>';
                    $datumTable.append(html);
                }
            }
            $.get(
                    "{:U('queryDatumEnclosure')}",
                    {
                        datum_dic_class:datumDicClass,
                        datum_id:data.datum_id
                    },
                    function(data){
                        if(data.status == 1){
                            var base_url = "{:C('ENCLOSURE_URL')}";
                            $('.enclosureDiv').show();
                            $('#enclosures').empty();
                            var enclosures = data.data;
                            for(var i in enclosures){
                                var url = base_url + enclosures[i].enclosure_url;
                                var html = '<li><a target="_blank" href="' + url + '">'+ enclosures[i].enclosure_name +'</a></li>';
                                $('#enclosures').append(html);
                            }
                        }else{
                            $('.enclosureDiv').hide();
//                            layer.msg(data.msg);
                        }
                    }
            )

        }


        var TreeHelper = function(){
            var oTreeHelper = new Object();

            oTreeHelper.init = function(data){
                datumDicData = data;
                $tree.treeview({
                    data:data,
                    silent:true,
                    showBorder:false,
                    levels:1,
                    onNodeSelected:function (event, data) {
                        datumDicClass = data.class_id;
                        if(data.type_code){
                            datumDicType = data.type_code;
                        }else{
                            datumDicType = 0;
                        }
                        $('#placeHolder').hide();
                        $('#table').show();
                        $.get(
                                "{:U('getTableFields')}",
                                {datum_dic_class:datumDicClass},
                                function (data) {
                                    if(data.status == 1){
                                        var fields = data.data;
                                        tableHelper.init(fields);
                                    }else{
                                        layer.msg(data.msg);
                                    }
                                }
                        )

                    }

                });
            };

            oTreeHelper.refresh= function(){
                $.ajax({
                    type:'post',
                    url:"{:U('getDatumTree')}",

                    success:function(data){
                        if(data.status == 1){
                            oTreeHelper.init(data.data);
                            updateTreeHeight();
                        }else{
                            layer.msg(data.msg);
                        }
                    }
                });
            };
            return oTreeHelper;
        };

        function updateTreeHeight(){
            var height = $('#treeContrainer').parent().height();
            $('#tree-container').css('height',height + '.px');
        }




        var TableHelper = function(){
            var oTableHelper = new Object();

            oTableHelper.selections = [];
            oTableHelper.currentFields = [];

            oTableHelper.init = function(fields){
                for(var i in fields){
                    fields[i]['width'] = 200;
                    switch(fields[i].field){
                        case 'name':
                            fields[i].formatter = function(value){

                                return '<a href="javascript: void 0" class="name_click">' + value + '</a>';
                            };
                            fields[i]['events'] = operateEvents
                            break;

                        case 'chech_status':
                            fields[i].formatter = function(val){
                                var text = val;
                                if(val == -1){
                                    text = '审核未通过';
                                }else if(val == 0){
                                    text = '未审核';
                                }else if(val == 1){
                                    text = '审核通过';
                                }
                                return text;
                            };
                            break;
                        case 'version_flag':
                            fields[i].formatter = function(val){
                                var text = val;
                                if(val == 0){
                                    text = '发布';
                                }else if(val == 1){
                                    text = '修订';
                                }
                                return text;
                            };
                            break;

                        case 'operation_flag':
                            fields[i].formatter = function(val){
                                var text = val;
                                if(val == 0){
                                    text = '没有';
                                }else if(val == 1){
                                    text = '有';
                                }
                                return text;
                            };
                            break;


                    }


//                    if(fields[i].field == 'name'){
//                        fields[i]['width'] = 200;
//                        fields[i].formatter = function(value){
//
//                            return '<a href="javascript: void 0" class="name_click">' + value + '</a>';
//                        };
//                        fields[i]['events'] = operateEvents
//                    }else{
//                        fields[i]['width'] = 200;
//                    }
//                    var field = fields[i].field;
//                    val = value;
//                    var text;
//
//                    switch (field){
//                        case 'chech_status':
//                            if(val == -1){
//                                text = '审核未通过';
//                            }else if(val == 0){
//                                text = '未审核';
//                            }else if(val == 1){
//                                text = '审核通过';
//                            }
//                            return text;
//                        case 'version_flag':
//                            if(val == 0){
//                                text = '发布';
//                            }else if(val == 1){
//                                text = '修订';
//                            }
//                            return text;
//                        case 'operation_flag':
//                            if(val == 0){
//                                text = '没有';
//                            }else if(val == 1){
//                                text = '有';
//                            }
//                            return text;
//                    }
                }
                oTableHelper.currentFields = fields;
                $table.bootstrapTable("destroy");
                $table.bootstrapTable({
                    formatLoadingMessage: function () {
                        return '正在努力地加载数据中，请稍候……';
                    },
                    formatRecordsPerPage: function (pageNumber) {
                        return '每页显示 ' + pageNumber + ' 条记录';
                    },
                    formatShowingRows: function (pageFrom, pageTo, totalRows) {
                        return '显示第 ' + pageFrom + ' 到第 ' + pageTo + ' 条记录，总共 ' + totalRows + ' 条记录';
                    },
                    formatSearch: function () {
                        return '名称搜索';
                    },
                    formatNoMatches: function () {
                        return '没有找到匹配的记录';
                    },
                    formatPaginationSwitch: function () {
                        return '隐藏/显示分页';
                    },
                    formatRefresh: function () {
                        return '刷新';
                    },
                    formatToggle: function () {
                        return '切换';
                    },
                    formatColumns: function () {
                        return '列';
                    },
                    responseHandler:function(res){
                        return res;
                    },
                    url:"{:U('dataList')}",
                    method:'get',
                    striped:true,
                    cache:false,
                    pagination:true,
                    sortable:false,
                    sortOrder:'asc',
                    sidePagination:'server',
                    queryParams:oTableHelper.queryParams,
                    pageNumber:1,
                    pageSize:10,
                    pageList:[10,20,50],
                    uniqueId:'datum_id',
                    search:true,
                    height:getHeight(),
                    columns:oTableHelper.currentFields
                });


                $(window).resize(function () {
                    $table.bootstrapTable('resetView', {height:getHeight()});
                });

                $table.on('check.bs.table uncheck.bs.table ' +
                        'check-all.bs.table uncheck-all.bs.table', function () {
                    $('#remove').prop('disabled', !$table.bootstrapTable('getSelections').length);

                    // save your data, here just save the current page
                    oTableHelper.selections = oTableHelper.getIdSelections();
                    // push or splice the selections if you want to save all data selections
                });
            };

            oTableHelper.queryParams = function(params){
                var temp = {
                    limit:params.limit,
                    offset:params.offset,
                    search:params.search,
                    datum_dic_class:datumDicClass,
                    datum_dic_type:datumDicType
                };
                return temp;
            };

            oTableHelper.getIdSelections = function(){
                return $.map($table.bootstrapTable('getSelections'), function (row) {
                    return row.user_id;
                })
            };

            oTableHelper.refresh = function(){
                $table.bootstrapTable('refresh');
            };

            return oTableHelper;
        };

        function getHeight(){
            return $('#right').height() - 30;
        }

        function initValidate(){
            $('#importForm').validate({
                debug:true,
                submitHandler:function (form) {
                    var url = $(form).prop('action');
//                    var postData = $(form).serialize();
//                    $.post(url,postData, function(data){
//                        if(data.status == 1){
//                            layer.msg('编辑成功');
//                            form.reset();
//                            clearImageView();
//                            $complexModal.modal('hide');
//                            $table.bootstrapTable('refresh');
//                        }else{
//                            layer.msg('编辑失败');
//                        }
//                    });
                    $(form).ajaxSubmit({
                        url:url,
                        success:function(data){
                            if(data.status == 1){
                                layer.msg('导入成功');
                                $importModal.modal('hide');
                            }else{
                                layer.msg(data.msg);
                            }
                        }
                    })

                },
                rules:{
                    class_id:"required",
                    file_import:'required'
                },
                messages:{
                    class_id:'请选择类型',
                    file_import:'请选择上传文件'
                }
            });
        }

        $(function () {
            oTreeHelper = new TreeHelper();
            tableHelper = new TableHelper();

            oTreeHelper.refresh();
            initValidate();
            $('#group-import').click(function () {
                $importModal.modal('show');
                $('#class_id').val(datumDicClass);
            });

//            $('#treeContrainer').resize(function () {
//                updateTreeHeight();
//            });

//            $('#importForm').submit(function () {
//                var url = $(this).prop('action');
//                $(this).ajaxSubmit({
//                    url:url,
//                    beforeSubmit:function(){
//                        var file = $('#file_import').val();
//
//                        var selectClassId = $('#class_id').val();
//                        return false;
//                    },
//                    success:function(data){
//
//                    },
//                    resetForm:true,
//                    dataType:json
//                });
//
//                return false;
//            })

        })
    </script>

</block>